package cc.fs.zhihu

import android.app.Application
import cc.fs.zhihu.beans.FileBean
import cc.fs.zhihu.utils.DatabaseHelper
import java.lang.Deprecated
import java.util.*

/**
 * Created by fostion on 2015/10/13.
 */
class AppContext : Application() {

    companion object {
        val selectFiles: ArrayList<FileBean> = ArrayList();
        var instanceTem: AppContext ? = null
        val instance: AppContext by lazy {
            instanceTem!!
        }

        fun isContainFile(currentFile: FileBean): Boolean {
            for (file in selectFiles) {
                if (file.name!!.equals(currentFile.name))
                    return true
            }
            return false
        }

        fun addSelectFile(currentFile: FileBean) {
            if (!isContainFile(currentFile)) {
                selectFiles.add(currentFile)
                databaseHelper.saveFile(currentFile)
            }
        }

        fun removeSelectFile(currentFile: FileBean) {
            for (file in selectFiles) {
                if (file.name!!.equals(currentFile.name)) {
                    selectFiles.remove(file)
                    databaseHelper.removeFile(currentFile)
                    return
                }
            }
        }

        fun clearMemory(){
            selectFiles.clear()
        }

        //init databaseHelper
        val databaseHelper:DatabaseHelper by lazy {
            DatabaseHelper(instance)
        }
    }

    override fun onCreate() {
        super.onCreate()
        instanceTem = this
        selectFiles.clear()
        selectFiles.addAll(databaseHelper.getFiles())
    }
}